package com.wits.qso.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.HomeworkHViewDaoCustom;
import com.wits.qso.entity.HomeworkHView;
import com.wits.qso.pageable.NativeSqlPageable;

@Component
public class HomeworkHViewDaoImpl extends BaseDaoImpl<HomeworkHView, String>
		implements HomeworkHViewDaoCustom {

	@SuppressWarnings("unchecked")
	public List<HomeworkHView> findHomeworkHViews(Map<String, Object> param) {
		List<HomeworkHView> list = new ArrayList<HomeworkHView>();
		if (param == null) {
			return list;
		}
		StringBuffer buf = new StringBuffer();
		buf.append(" SELECT DISTINCT a.hwkh_id,a.date_cre,a.date_upd, a.remark, a.user_cre,a.user_upd,a.hwkh_file,a.hwkh_title,a.hwkh_url,d.dept_title,e.grade_title,f.cls_title,g.course_id,g.course_title,h.ter_id,h.ter_name FROM qso_homework_h a LEFT JOIN qso_crecontent b ON a.hwkh_id=b.crecnt_hwkhid LEFT JOIN qso_btable c ON b.crecnt_btid=c.btable_id LEFT JOIN qso_dept d ON c.btable_dptid=d.dept_id LEFT JOIN qso_grade e ON c.btable_gradeid=e.grade_id LEFT JOIN qso_class f ON c.btable_claid=f.cls_id LEFT JOIN qso_course g ON c.btable_couid=g.course_id LEFT JOIN qso_teacher h ON c.btable_teaid=h.ter_id where 1=1 ");

		if (param.get("hwkh_id") != null) {
			buf.append(" and a.hwkh_id=:hwkh_id ");
		}
		if (param.get("cls_id") != null) {
			buf.append(" and f.cls_id=:cls_id ");
		}
		Query query = em.createNativeQuery(buf.toString(), HomeworkHView.class);
		if (param.get("hwkh_id") != null) {
			query.setParameter("hwkh_id", param.get("hwkh_id"));
		}
		if (param.get("cls_id") != null) {
			query.setParameter("cls_id", param.get("cls_id"));
		}
		list = query.getResultList();
		return list;
	}

	
	@SuppressWarnings("unchecked")
	public Page<HomeworkHView> findHomeworkHViewPages(Map<String, Object> param) {
		PageImpl<HomeworkHView> pipl=null;
		if (param == null) {
			 return pipl;
		}
		
		StringBuffer buf = new StringBuffer();
		buf.append(" SELECT DISTINCT a.hwkh_id,a.date_cre,a.date_upd, a.remark, a.user_cre,a.user_upd,a.hwkh_file,a.hwkh_title,a.hwkh_url,d.dept_title,e.grade_title,f.cls_title,g.course_id,g.course_title,h.ter_id,h.ter_name FROM qso_homework_h a LEFT JOIN qso_crecontent b ON a.hwkh_id=b.crecnt_hwkhid LEFT JOIN qso_btable c ON b.crecnt_btid=c.btable_id LEFT JOIN qso_dept d ON c.btable_dptid=d.dept_id LEFT JOIN qso_grade e ON c.btable_gradeid=e.grade_id LEFT JOIN qso_class f ON c.btable_claid=f.cls_id LEFT JOIN qso_course g ON c.btable_couid=g.course_id LEFT JOIN qso_teacher h ON c.btable_teaid=h.ter_id where 1=1 ");

		if (param.get("hwkh_id") != null) {
			buf.append(" and a.hwkh_id=:hwkh_id ");
		}
		if (param.get("cls_id") != null) {
			buf.append(" and f.cls_id=:cls_id ");
		}
		
		//查询总数
		StringBuffer sqlBuf00=new StringBuffer("");
		sqlBuf00.append(buf);
		sqlBuf00.append(" ");
		//分页查询起始行，每页行数
		StringBuffer sqlBuf01=new StringBuffer();
		sqlBuf01.append(buf);
		String[] orderByArray=(String[])param.get("orderByArray");
		if(orderByArray!=null&&orderByArray.length>0){
			sqlBuf01.append(" order by ");
			for(int i=0;i<orderByArray.length;i++){
				String str=orderByArray[i];
				if(str!=null){
					if(i>0){
						sqlBuf01.append(",");
					}
					sqlBuf01.append(str);
				}
			}
		}
		sqlBuf01.append(" limit :startLine,:lineEveryPage ");
		
		Query query00 = em.createNativeQuery(sqlBuf00.toString(),HomeworkHView.class);
		Query query = em.createNativeQuery(sqlBuf01.toString(), HomeworkHView.class);
		
		if (param.get("hwkh_id") != null) {
			query00.setParameter("hwkh_id", param.get("hwkh_id"));
			query.setParameter("hwkh_id", param.get("hwkh_id"));
		}
		if (param.get("cls_id") != null) {
			query00.setParameter("cls_id", param.get("cls_id"));
			query.setParameter("cls_id", param.get("cls_id"));
		}
		
		NativeSqlPageable ctp=getNativeSqlPageable(param);
		int pageNum=ctp.getPageNumber();
		int pageSize=ctp.getPageSize();
		query.setParameter("startLine", (pageNum-1)*pageSize);
		query.setParameter("lineEveryPage", pageSize);
		
		//查询总数
		int total=query00.getResultList().size();
		//查询记录
		@SuppressWarnings("rawtypes")
		List list=query.getResultList();
		
		pipl=new PageImpl<HomeworkHView>(list, ctp, total);
		return 	pipl;
	}
	
	private NativeSqlPageable getNativeSqlPageable(Map<String,Object> param){
		NativeSqlPageable ctp=new NativeSqlPageable();
		Integer pageNumber=param.get("pageNumber")==null?new Integer(1):(Integer)param.get("pageNumber");
		Integer pageSize=param.get("pageSize")==null?new Integer(20):(Integer)param.get("pageSize");
		ctp.setPageNumber(pageNumber.intValue());
		ctp.setPageSize(pageSize.intValue());
		return ctp;
	}
}
